package com.zsw.dubbo.console;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;

import javax.xml.bind.PropertyException;

import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

import com.alibaba.dubbo.config.spring.ServiceBean;
import com.zsw.dubbo.util.StringUtil;


public class MainConsole {
	public static String help = "显示所有控制台默认命令";
	public static String helpu = "显示所有用户可操作命令";
	public static String thread = "显示所有当前线程";
	public String exit = "停止当前java进程";

	public static String helpu() {
		SysConsole sc = SysConsole.getSysConsole();
		Properties pps = sc.getPps();
		Set<Object> keys = new TreeSet<Object>(pps.keySet());
		for (Object key : keys) {
			Console c = new Console(key.toString());
			ConsoleB cb = null;
			try {
				// cb.init();
				cb = c.analyze();
			} catch (NullPointerException e) {
				System.err.println(e.getMessage());
			} catch (PropertyException e) {
				System.err.println(e.getMessage());
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (NoSuchFieldException e) {
				e.printStackTrace();
			}
			if (cb != null) {
				if (cb.getClazz() == null) {
					cb.setMessage("#类没有被找到----该命令当前状态无效");
				}
				System.out.println(StringUtil.strAdd(key.toString(), " ",
						StringUtil.ALIGN_LEFT, 20)
						+ " : "
						+ (c.getProValue() != null ? c.getProValue() : (cb
								.getClassName() + "." + cb.getMethodName()))
						+ "  >>>" + cb.getMessage());
			}
		}
		return null;
	}

	public static String help() {
		SysConsole sc = SysConsole.getSysConsole();
		Map<String, ConsoleB> cbMap = sc.getMethods();
		Set<String> keys = new TreeSet<String>(cbMap.keySet());
		for (String key : keys) {
			System.out.println(StringUtil.strAdd(key, " ",
					StringUtil.ALIGN_LEFT, 20)
					+ " : "
					+ cbMap.get(key).getMessage());
		}
		return null;
	}

	public static String thread() {
		Map<Thread, StackTraceElement[]> threadMap = Thread.getAllStackTraces();
		Set<Thread> threads = threadMap.keySet();
		for (Thread thread : threads) {
			System.out.println(thread.getName());
		}
		return null;
	}
	public static String a() {
		Map<Thread, StackTraceElement[]> threadMap = Thread.getAllStackTraces();
		Set<Thread> threads = threadMap.keySet();
		System.out.println(">>>>>>>>>>>>>>"+threads.size());
		for (Thread thread : threads) {
			System.out.println(thread.getName());
		}
		return null;
	}
	@SuppressWarnings("rawtypes")
	public static String autowireBeans(String path,String type) {
		if(SysConsole.app != null){
			AutowireCapableBeanFactory fac = SysConsole.app.getAutowireCapableBeanFactory();
			if(fac instanceof BeanDefinitionRegistry){
				Resource resourse = null;
				if(type.equalsIgnoreCase("class")){
					resourse = new ClassPathResource(path);
				}else {
					resourse = new FileSystemResource(path);
				}
				XmlBeanDefinitionReader  reader = new XmlBeanDefinitionReader((BeanDefinitionRegistry) fac);  
				reader.loadBeanDefinitions(resourse);
				Map<String, ServiceBean> beans = SysConsole.app.getBeansOfType(ServiceBean.class);//如果有重复服务，则都会变为未暴露状态
				List<String> list = new ArrayList<String>();
				for(String s_ : beans.keySet()){
					if(beans.get(s_).isExported()){
						list.add(beans.get(s_).getInterface());
					}
				}
				for(String s_ : beans.keySet()){
					ServiceBean bean = beans.get(s_);
					if(!bean.isExported() && !list.contains(bean.getInterface())){//避免同一个服务重复暴露
						bean.export();
						list.add(bean.getInterface());
					}else {
						fac.destroyBean(bean);
					}
				}
				return "操作成功！";
			}
		}
		return "failure";
	}

	public static void exit() {
		System.out.println("当前虚拟机进程即将结束");
		System.exit(0);
	}
}
